package xan.tm.digital.common.entity;

import lombok.Data;
import xan.tm.digital.common.enums.ErrorCodeEnum;

import java.io.Serializable;


/**
 * 通用返回类 * @date 2023/06/17
 */
@Data
public class BaseResponse<T> implements Serializable {

    private int code; // 状态码

    private T data; // 数据

    private String message; // 响应信息

    public BaseResponse(int code, T data, String message) {
        this.code = code;
        this.data = data;
        this.message = message;
    }

    public static <T> BaseResponse<T> success(T data){
        return new BaseResponse<T>(ErrorCodeEnum.SUCCESS.getCode(), data, ErrorCodeEnum.SUCCESS.getMessage());
    }

    public static <T> BaseResponse<T> fail(ErrorCodeEnum errorCodeEnum, String message){
        return new BaseResponse<T>(errorCodeEnum.getCode(), null, message);
    }

    public BaseResponse(int code, T data) {
        this(code, data, "");
    }

    public BaseResponse(ErrorCodeEnum errorCodeEnum) {
        this(errorCodeEnum.getCode(), null, errorCodeEnum.getMessage());
    }
}
